<?php

class CCoreBaseConvert
{
	#<editor-fold defaultstate="collapsed" desc="construct (private)">
	private function __construct() {}
	#</editor-fold>
	
	/**
	 * @param	string	$sText
	 * @param	string	$sFromCharset
	 * @param	string	$sToCharset
	 * @return	string
	 */
	public static function Encoding($sText, $sFromCharset, $sToCharset)
	{
		CCore::CheckTypes(array('$sText', '$sFromCharset', '$sToCharset'));
		
		if (strtolower($sFromCharset) === strtolower($sToCharset) || empty($sText) 
			|| self::IsASCII($sText)
			)
		{
			return $sText;
		}
		
		$sFromCharsetLower = strtolower($sFromCharset);
		$sToCharsetLower = strtolower($sToCharset);
		
		if (('iso-8859-1' === $sFromCharsetLower || 'ansi' === $sFromCharsetLower)
			&& ('utf-8' === $sToCharsetLower) && function_exists('utf8_encode'))
		{
			return utf8_encode($sText);
		}

		if (('iso-8859-1' === $sToCharsetLower ||  'ansi' === $sToCharsetLower)
			&& ('utf-8' === $sFromCharsetLower) && function_exists('utf8_decode'))
		{
			return utf8_decode($sText);
		}
		
		if ('ks_c_5601-1987' === $sFromCharsetLower)
		{
			$sFromCharset = 'EUC-KR';
			$sFromCharsetLower = 'euc-kr';
		}

		$mResult = @iconv($sFromCharset, $sToCharset.'//IGNORE', $sText);
		return (false === $mResult) ? $sText : $mResult;
	}
	
	/**
	 * @param	string	$sText
	 * @return	bool
	 */
	public static function IsASCII($sText)
	{
		CCore::CheckTypes(array('$sText'));

		return (0 < preg_match('/^([\x09\x0A\x0D\x20-\x7E])*$/xs', $sText));
	}
}